﻿using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace CNKI.TPI.Web.Base
{
    public enum MediaType
    {
        /// <summary>
        /// 论文，期刊
        /// </summary>
        Doc = 0,
        /// <summary>
        /// 视频
        /// </summary>
        Video = 4,
        /// <summary>
        /// 图片
        /// </summary>
        Pic = 5,
        /// <summary>
        /// 音频
        /// </summary>
        Audio = 6,
        /// <summary>
        /// 电子书
        /// </summary>
        Ebook = 1,
        /// <summary>
        /// 新闻
        /// </summary>
        News=11,
    };
    public enum DownFromType
    {
        /// <summary>
        /// 正常下载
        /// </summary>
        CommonDown =0,
        /// <summary>
        /// 审核下载
        /// </summary>
        AuditDown = 1,
        /// <summary>
        /// 接口下载
        /// </summary>
        InterfaceDown = 2,
    };
    public enum BrowseFromType
    {
        /// <summary>
        /// 正常浏览
        /// </summary>
        CommonBrowse = 0,
        /// <summary>
        /// 审核浏览
        /// </summary>
        AuditBrowse = 1,
        /// <summary>
        /// 接口浏览
        /// </summary>
        InterfaceBrowse = 2,
    };
    public class ResourceHelper
    {
        //private static string baseUrl = System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
        public static string baseUrl = ConfigHelper.GetFileServerUrl() =="localhost"? System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/" : ConfigHelper.GetFileServerUrl().TrimEnd('/') + "/";

        /// <summary>
        /// 获取数字对象图片
        /// </summary>
        /// <param name="dbID"></param>
        /// <param name="itemID"></param>
        /// <returns></returns>
        public static string GetDigitalPicURL(int dbID, int resourceID)
        {
            return baseUrl + String.Format("File/GetDigitalPic?dbId={0}&fileId={1}&key={2}", dbID, resourceID, EncryptHelper.Encrypt(dbID.ToString() + resourceID.ToString()));
        }

        /// <summary>
        /// 获取图像采样
        /// </summary>
        /// <param name="dbID"></param>
        /// <param name="itemID"></param>
        /// <param name="resourceID"></param>
        /// <returns></returns>
        public static string GetSmallPicURL(int dbID, int resourceID)
        {
            return baseUrl + String.Format("File/GetSmallPic?dbId={0}&fileId={1}&key={2}", dbID, resourceID, EncryptHelper.Encrypt(dbID.ToString() + resourceID.ToString()));
        }

        /// <summary>
        /// 获取记录的第一个图像采样
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public static string GetFirstSmallPicURL(int dbId, int recordId)
        {
            return baseUrl + String.Format("File/GetFirstSmallPic?dbId={0}&recordId={1}&key={2}", dbId, recordId, EncryptHelper.Encrypt(dbId.ToString() + recordId.ToString()));
        }

        /// <summary>
        /// 获取记录的第一个图片数字对象
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public static string GetFirstDigitalPicURL(int dbId, int recordId)
        {
            return baseUrl + String.Format("File/GetFirstDigitalPic?dbId={0}&recordId={1}&key={2}", dbId, recordId, EncryptHelper.Encrypt(dbId.ToString() + recordId.ToString()));
        }

        /// <summary>
        /// 根据fileid获取下载路径
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        public static string GetFileDownloadURL(int dbId, int fileid,int recordid,int downtype)
        {
            return baseUrl + String.Format("File/GetDigitalFile?dbId={0}&fileId={1}&key={2}&downtype={3}&recordId={4}", dbId, fileid, EncryptHelper.Encrypt(dbId.ToString() + fileid.ToString()),downtype,recordid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="recordId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetFileDownloadURLByRecoredIdAndType(int dbId, string recordId, MediaType type)
        {
            return baseUrl + String.Format("File/GetFirstDigitalFileByType?dbId={0}&recordId={1}&key={2}&type={3}", dbId, recordId, EncryptHelper.Encrypt(dbId.ToString() + recordId.ToString()), type);
        }

        public static string GetFileDownloadURLByRecoredId(int dbId, int recordId,int downtype)
        {
            return baseUrl + String.Format("File/GetFirstDigitalFile?dbId={0}&recordId={1}&key={2}&downtype={3}", dbId, recordId, EncryptHelper.Encrypt(dbId.ToString() + recordId.ToString()),downtype);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        public static string GetAudioStreamURL(int dbId, int resourceId)
        {
            return baseUrl + String.Format("File/GetStream?type=6&dbId={0}&fileId={1}&key={2}", dbId, resourceId, EncryptHelper.Encrypt(dbId.ToString() + resourceId.ToString()));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        public static string GetVideoStreamURL(int dbId, int resourceId)
        {
            return baseUrl + String.Format("File/GetStream?type=4&dbId={0}&fileId={1}&key={2}", dbId, resourceId, EncryptHelper.Encrypt(dbId.ToString() + resourceId.ToString()));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="recordId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetFirstStreamURL(int dbId, int recordId, MediaType type)
        {
            return baseUrl + String.Format("File/GetFirstStream?dbId={0}&recordId={1}&key={2}&type={3}", dbId, recordId, EncryptHelper.Encrypt(dbId.ToString() + recordId.ToString()), type);
        }

        public static MediaType ConvertMediaType(int templateID)
        {
            MediaType result = MediaType.Doc;
            int videoLibID = int.Parse(ConfigHelper.GetAppSettingsValue("VideoLibID"));
            int picLibID = int.Parse(ConfigHelper.GetAppSettingsValue("PicLibID"));
            int audioLibID = int.Parse(ConfigHelper.GetAppSettingsValue("AudioLibID"));
            int ebookLibID = int.Parse(ConfigHelper.GetAppSettingsValue("EBookLibID"));
            if (templateID == videoLibID)
            {
                result = MediaType.Video;
            }
            else if (templateID == picLibID)
            {
                result = MediaType.Pic;
            }
            else if (templateID == audioLibID)
            {
                result = MediaType.Audio;
            }
            else if (templateID == ebookLibID)
            {
                result = MediaType.Ebook;
            }
            return result;
        }

        public static string GetSingleSearchDetailURL(SYS_CMS_DATABASE dbInfo, DataRow dr)
        {
            string hostUrl = System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
            string url = "";
            switch ((SearchType)dbInfo.SearchType)
            {
                case SearchType.TPI:
                    if (dbInfo.DatabaseType == (int)MediaType.News) //如果是新闻类型则跳转到新闻细缆页面
                    {
                        url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"]+"&type="+(int)MediaType.News;
                    }
                    else
                    {
                        url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    }
                    break;
                case SearchType.CNKI:
                    url = ConfigHelper.GetCNKIResourceHost() + "/detail.aspx?dbname=" + dbInfo.DatabaseCode + "&filename=" + dr[ConfigHelper.GetCNKIResourceKey()];
                    break;
                case SearchType.KSPIRDER:
                    url = ConfigHelper.GetKspiderHost() + "/HBrowseDetail.aspx?id=" + dr[ConfigHelper.GetKspiderKey()];
                    break;
                default:
                    url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    break;
            }
            return url;
        }

        public static string GetSingleSearchDetailURLForFav(SYS_CMS_DATABASE dbInfo, DataRow dr)
        {
            string hostUrl = System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
            string url = "";
            switch ((SearchType)dbInfo.SearchType)
            {
                case SearchType.TPI:
                    if (dbInfo.DatabaseType == (int)MediaType.News) //如果是新闻类型则跳转到新闻细缆页面
                    {
                        url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"] + "&type=" + (int)MediaType.News;
                    }
                    else
                    {
                        url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    }
                    break;
                case SearchType.CNKI:
                    url = ConfigHelper.GetCNKIResourceHost() + "/detail.aspx?dbname=" + dbInfo.DatabaseCode + "&filename=" + dr["SYS_FLD_SYSID"];
                    break;
                case SearchType.KSPIRDER:
                    url = ConfigHelper.GetKspiderHost() + "/HBrowseDetail.aspx?id=" + dr["SYS_FLD_SYSID"];
                    break;
                default:
                    url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    break;
            }
            return url;
        }

        public static string GetMultiSearchDetailURL(SYS_CMS_DATABASE dbInfo, DataRow dr)
        {
            string hostUrl = System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
            string url = "";
            switch ((SearchType)dbInfo.SearchType)
            {
                case SearchType.TPI:
                    url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    break;
                case SearchType.CNKI:
                    url = ConfigHelper.GetCNKIResourceHost() + "/detail.aspx?dbname=" + dbInfo.DatabaseCode + "&filename=" + dr["SYS_FLD_SYSID"];
                    break;
                case SearchType.KSPIRDER:
                    url = url = ConfigHelper.GetKspiderHost() + "/HBrowseDetail.aspx?id=" + dr["SYS_FLD_SYSID"];
                    break;
                default:
                    url = hostUrl + "Detail?dbID=" + dbInfo.SysID + "&dbName=" + dbInfo.DatabaseCode + "&sysID=" + dr["SYS_FLD_SYSID"];
                    break;
            }
            return url;
        }

        public static bool CheckFileExist(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return false;
            }
            string url = string.Format("path={0}&key={1}", HttpUtility.UrlEncode(path), EncryptHelper.Encrypt(path));

            string result = null;
            if (!baseUrl.ToLower().StartsWith("http"))
            {
                string kbaseServerUrl = string.Format("{0}://{1}{2}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Authority, baseUrl);
                result = BaseHttpClient.CallInterfaceGet(kbaseServerUrl + "File/CheckFileExist", url);
            }
            else
            {
                result = BaseHttpClient.CallInterfaceGet(baseUrl + "File/CheckFileExist", url);
            }
            
            return "true" == result ? true : false;
        }

        public static string GetOnlineViewUrl(int dbid, int fileid, int downType, string dbName, int recordid)
        {
            if (!baseUrl.ToLower().StartsWith("http"))
            {
                string kbaseServerUrl = string.Format("{0}://{1}{2}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Authority, baseUrl);
                return kbaseServerUrl + string.Format("DocResource/FileRead?dbId={0}&fileId={1}&downType={2}&dbName={3}&recordID={4}", dbid, fileid, downType, dbName, recordid);
            }
            else
            {
                return baseUrl + string.Format("DocResource/FileRead?dbId={0}&fileId={1}&downType={2}&dbName={3}&recordID={4}", dbid, fileid, downType, dbName, recordid);
            }
        }
    }
}
